#! /bin/sh

# **Task Protocol Request Demo**
# REQUEST TP/1.0\r\n
# ID: 1\r\n
# Length: 18\r\n
# \r\n
# 123456789012345678

# **Task Protocol Reponse Demo**
# RESPONSE TP/1.0\r\n
# ID: 1\r\n
# Length: 18\r\n
# \r\n
# 123456789012345678

# Would be set by function request()
ID=""
TASK=""

# Would be set by function work()
RESULT=""

# User define their own work function,
# and set WORK_FUNC.
WORK_FUNC=""

function request()
{
	IFS="\r"
	read LINE
	REQUEST_LINE=`echo -en $LINE | tr -d "\r"`

	IFS=": "
	read VNAME VALUE
	ID=`echo -en $VALUE | tr -d "\r"`

	read VNAME VALUE
	LENGTH=`echo -en $VALUE | tr -d "\r"`

	read

	IFS=""
	read -N $LENGTH TASK
}

function response()
{
	echo -ne 'RESPONSE TP/1.0\r\n'
        echo -ne 'ID: '$ID'\r\n'
        echo -ne 'Length: '${#RESULT}'\r\n'
        echo -ne '\r\n'
        echo -ne $RESULT
}

function mainloop()
{
	while test 1
	do
		request
		$WORK_FUNC
		response
	done
}
